package com.qing.demo.netty;

import cn.hutool.json.JSONUtil;
import com.qing.demo.netty.client.ClientSocket;
import com.qing.demo.netty.future.SyncWrite;
import com.qing.demo.netty.msg.Request;
import com.qing.demo.netty.msg.Response;
import io.netty.channel.ChannelFuture;

/**
 * @author caozhipeng
 * @date 2025/8/2
 */
public class StartClient {

    private static ChannelFuture future;

    public static void main(String[] args) {

        ClientSocket client=new ClientSocket();
        new Thread(client).start();

        while (true){
            try {
                if(null==future){
                    future=client.getFuture();
                    Thread.sleep(500);
                    continue;
                }
                Request request=new Request();
                request.setResult("查询用户信息");

                SyncWrite syncWrite=new SyncWrite();
                Response response=syncWrite.writeAndSync(future.channel(), request,1000);
                System.out.println("服务端返回结果："+ JSONUtil.toJsonStr(response));
                Thread.sleep(1000);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
